package com.cyt.base.util;

import org.apache.commons.codec.binary.Base64;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 *@Author: YuShengQiang
 *@Date: 2018-11-16 17:30
 *@Description: 通用方法
 */
public class BaseUtil {
    public static final String NUMBER_CONTENT="0123456789";
    public static final String LETTER_CONTENT="abcdefghijklmnopqrstuvwxyz";
    public static final String LETTER_NUMBER_CONTENT="0123456789abcdefghijklmnopqrstuvwxyz";
    public static final String UPPERCASE_LETTER_NUMBER_CONTENT="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z" };
    //获取文件名称
    public static String acceptFileName(String fileType){
        String  fileName = DateUtil.DateToStr(new Date(), DateUtil.DATE_TIME_PATTERN_2)+ BaseUtil.acceptRandom(BaseUtil.NUMBER_CONTENT,6)+"."+fileType;
        return fileName;
    }

    //获取文件目录
    public static String acceptFileDir(String realPath,String dir){
        //如果文件夹不存在则创建
        File dicFile =new File(realPath,dir);
        if(!dicFile .exists()  && !dicFile .isDirectory()){
            dicFile .mkdirs();
        }
        return dicFile.getAbsolutePath();
    }

    //获取文件的后缀
    public static String acceptFileType(String fileName){
        String suffix = fileName.indexOf(".")!=-1?fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()):null;
        return suffix;
    }
    //判断是否是图片
    public static boolean fileCheckImg(String type){
        if(!StringUtils.isEmpty(type)&&(type.equalsIgnoreCase("jpg")||type.equalsIgnoreCase("jpeg")||type.equalsIgnoreCase("gif")||type.equalsIgnoreCase("png"))){
            return true;
        }
        return false;
    }
    //判断是否是execl
    public static boolean fileCheckExecl(String type){
        if(!StringUtils.isEmpty(type)&&(type.equalsIgnoreCase("xls")||type.equalsIgnoreCase("xlsx"))){
            return true;
        }
        return false;
    }
    //获取随机数
    public static String acceptRandom(String content,int num){
        StringBuffer sb=new StringBuffer();
        for (int i=0;i<num;i++){
            int id=(int)((Math.random())*(content.length()-1));
            sb.append(content.substring(id,id+1));
        }
        return sb.toString();
    }
    //获取id
    public static  String uId(){
        return new Date().getTime()+acceptRandom(NUMBER_CONTENT,8);
    }
    //获取id
    public static  String uToken(){
        return new Date().getTime()+acceptRandom(NUMBER_CONTENT,28);
    }
    public static String urlEnodeUTF8(String str){
        String result = str;
        try {
            result = URLEncoder.encode(str,"UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取随机数
     * @param num
     * @param content
     * @return
     */
    public static String getRandom(int num,String content){
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<num;i++){
            int id=(int)(Math.random()*(content.length()-1));
            String code=content.substring(id,id+1);
            sb.append(code);
        }
        return sb.toString();
    }

    // 判断一个集合是否为空
    public static <T> boolean isEmpty(Collection<T> col) {
        if (col == null || col.isEmpty()) {
            return true;
        }

        return false;
    }

    // 判断一个集合是否不为空
    public static <T> boolean isNotEmpty(Collection<T> col) {
        return !isEmpty(col);
    }

    // 判断Map是否为空
    public static <K, V> boolean isEmpty(Map<K, V> map) {
        if (map == null || map.isEmpty()) {
            return true;
        }

        return false;
    }

    // 判断Map是否不为空为空
    public static <K, V> boolean isNotEmpty(Map<K, V> map) {
        return !isEmpty(map);
    }

    // 去除list中的重复数据
    public static <T> List<T> removeRepeat(List<T> list) {
        if (isEmpty(list)) {
            return list;
        }

        List<T> result = new ArrayList<T>();
        for (T e : list) {
            if (!result.contains(e)) {
                result.add(e);
            }
        }

        return result;
    }

    // 将集合转换为String数组
    public static <T> String[] toArray(List<T> list) {
        if (isEmpty(list)) {
            return null;
        }

        String[] result = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = String.valueOf(list.get(i));
        }

        return result;
    }

    public static String generateShortUuid() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();

    }
    /**
     * Base64 解码
     * @param name
     * @return
     */
    public static String decodeBase64(String name){
        try {
            return new String(Base64.decodeBase64(name),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * Base64 编码
     * @param name
     * @return
     */
    public static String encodeBase64String(String name){
        try {
            return Base64.encodeBase64String(name.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    //首字母转大写
    public static String toUpperCaseFirstOne(String s){
        if(Character.isUpperCase(s.charAt(0))){
            return s;
        }else{
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();

        }
    }
    //驼峰命名如：sys_user => sysUser
    public static String toTfmm(String str){
        String[] columList=str.split("_");
        StringBuffer s=new StringBuffer();
        if(columList!=null&&columList.length>0){
            for(int i=0;i<columList.length;i++){
                if(i==0){
                    s.append(columList[i]);
                }else{
                    s.append(BaseUtil.toUpperCaseFirstOne(columList[i]));
                }
            }
        }
        return s.toString();
    }
}
